package com.zhang.crm.controller;

import com.alibaba.excel.EasyExcel;
import com.zhang.crm.manager.ConvertCustomerManager;
import com.zhang.crm.pojo.TClue;
import com.zhang.crm.pojo.TTran;
import com.zhang.crm.pojo.req.ConvertClientReq;
import com.zhang.crm.result.R;
import com.zhang.crm.service.ClueService;
import com.zhang.crm.service.CustomerService;
import com.zhang.crm.service.TranService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class CustomerController {

    @Resource
    private ConvertCustomerManager convertCustomerManager;

    @Resource
    private ClueService clueService;

    @Resource
    private CustomerService customerService;

    @Resource
    private TranService tranService;

    @PostMapping("api/convertClientSubmit")
    public R convertCustomer(@RequestBody ConvertClientReq convertClientReq){
        Boolean b = convertCustomerManager.convertCustomerManager(convertClientReq);
        return b ? R.OK() : R.FAIL("添加失败");
    }

    @GetMapping("api/clueCustomerDetail/{id}")
    public R getClueById(@PathVariable("id")Integer id){
        TClue tClue = clueService.getClueCustomer(id);
        return R.OK(tClue);
    }

    /**
     * 返回前端文件
     * 批量下载
     * @param res
     * @return
     */
    @GetMapping("api/exportExcel")
    public void exportExcel(HttpServletResponse res) throws UnsupportedEncodingException {
        res.setContentType("application/octet-stream"); //文件 = 二进制流
        res.setHeader("Content-disposition", "attachment;filename=customer.xlsx");

        //生成文件
        customerService.downLoadCustomerExcel(res);

    }

    /**
     * 返回前端文件
     * 选择下载
     * @param res
     * @return
     */
    @GetMapping("api/chooseExportExcel")
    public void chooseExportExcel(@RequestParam(value = "ids",required = false) List<Integer> ids, HttpServletResponse res) throws UnsupportedEncodingException {
        res.setContentType("application/octet-stream"); //文件 = 二进制流
        res.setHeader("Content-disposition", "attachment;filename=chooseCustomer.xlsx");

        //生成文件
        customerService.chooseDownLoadCustomerExcel(res, ids);

    }
    @PostMapping("api/submitTran")
    public R createTran(@RequestBody TTran tTran){
        Boolean b = tranService.addTran(tTran);
        return b ? R.OK() : R.FAIL("添加失败");
    }



}
